﻿var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        chooseType:1,
        form: {
            ID:'',
            GoodsList:[{
                Name:'',
                Number:''
            }],
            GoodsUseName:'',
            ApprovalUserID:[],
            SendUserIDs:[],
            // examine_dec:'',
            Remark:'',
            action: 'savepurchaserequisition',
        },
        select_id_list:[],//选中的抄送人员(多选)
        choose_id_list:[],//选中的审批人员(多选)
        examine_writenum:0,

    },
    methods: {
        //提交
        do_save:function(){
            var that=this;
            var SendUserIDs=[];

            if (that.form.GoodsUseName==""){
                api.toast({
                    msg: '请填写物品用途',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            if (that.form.GoodsList.length<=0){
                api.toast({
                    msg: '请至少填写一条物品信息',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.GoodsList.length>0) {
                for (var i = 0; i < that.form.GoodsList.length; i++) {
                    if(that.form.GoodsList[i].Name==""){
                        api.toast({
                            msg: '请填写物品名称',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    if(that.form.GoodsList[i].Number==""){
                        api.toast({
                            msg: '请填写物品数量',
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                }
            }
            if(that.choose_id_list.length<=0){
                api.toast({
                    msg: '请选择审批人',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            // that.form.ApprovalUserID=that.choose_id_list[0].UserID;
            for (var i = 0; i < that.choose_id_list.length; i++) {
                var item = that.choose_id_list[i].UserID;
                that.form.ApprovalUserID.push(item);
            }
            that.form.ApprovalUserID=JSON.stringify(that.form.ApprovalUserID);

            for (var i = 0; i < that.select_id_list.length; i++) {
                var item = that.select_id_list[i].UserID;
                SendUserIDs.push(item);
            }
            that.form.SendUserIDs=JSON.stringify(SendUserIDs);
            ns.post(that.form, function (succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'purchase_updata',
                    });
                    api.sendEvent({
                        name:'reloadexaminelist'
                    })
                    setTimeout(() => {
                        api.closeWin();
                    }, 400);

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //审批申请限制字数
        examine_writeMsg:function(){
            var that=this;
            that.examine_writenum=that.form.Remark.length;
        },
        // 增加物品
        addbox:function(){
            this.form.GoodsList.push({
                Name:'',
                Number:''
            });
        },
        do_delete:function(item,index){
            this.form.GoodsList.splice(index,1);

        },
        open_more:function(type){
            var that=this;
            //审批全部
            if(type==1){
                ns.openWin('examine_Allperson', '全部审核人员', {
                    list: that.choose_id_list,
                    type:type,
                 }); 
            }else if(type==0){
                ns.openWin('examine_Allperson', '全部抄送人员', {
                    list: that.select_id_list,
                    type:type,
                 }); 
            }
        },
        //选择人
        choose_persons: function(IsApproval) {
            var that = this;
            that.chooseType=IsApproval;
            // IsApproval==0//抄送
            if(IsApproval==0){
                //chooseuser_frm
                // ns.openWin('chooseperson_frm', '请选择', {
                //         source: 'transfer',
                // }); 
                ns.openWin('chooseuser_frm', '选择抄送人', {
                    source: 'transfer',
                }); 
            }
            //IsApproval==1//审批
            if(IsApproval==1){
                // ns.openWin('chooseperson_frm', '请选择', {
                //    source: 'transfer',
                // });
                ns.openWin('chooseuser_frm', '选择审批人', {
                    source: 'transfer',
                }); 
            }
               
        },
        //移除抄送人
        guanbi_pic:function(id,IsApproval){
            var that=this;
            that.chooseType=IsApproval;
            if(that.chooseType==1){
                // that.choose_id_list.splice(index,1);
                that.choose_id_list = that.choose_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            if(that.chooseType==0){
                // console.log(JSON.stringify(that.select_id_list));
                // that.select_id_list.splice(index,1);
                that.select_id_list = that.select_id_list.filter((item) => {
                    return item.UserID != id
                    });
            }
            
        },
        // 抄送人
        select_person:function(select_list,type){
            var that=this;
            if(type==0){
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.select_id_list.push(item);
                }
            }else{
                for (var i = 0; i < select_list.length; i++) {
                    var item = select_list[i];
                    that.choose_id_list.push(item);
                }
            }
            
            let obj = {};
            if(type==0){
                let peon = that.select_id_list.reduce((cur,next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                },[]) ;
                that.select_id_list=peon;
            }else{
                let peon = that.choose_id_list.reduce((cur,next) => {
                    obj[next.UserID] ? "" : obj[next.UserID] = true && cur.push(next);
                    return cur;
                },[]) ;
                that.choose_id_list=peon;
            }
           
        }

    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    setTimeout(function () {
        app.form.AddCustomerName = api.getPrefs({
            sync: true,
            key: 'loginname'
        });
    }, 500)
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    api.addEventListener({
        name: 'purchase_add'
    }, function (ret) {
        if( app.chooseType==1){
            var chooselist=ret.value.data;
            app.select_person(chooselist,1);
            // app.choose_id_list=ret.value.data;
        }
        if( app.chooseType==0){

            var select_list=ret.value.data;
            app.select_person(select_list,0);
        }

    })
    api.addEventListener({
        name: 'purchase_update'
    }, function (ret) {
        if( ret.value.type==1){
            app.choose_id_list=ret.value.data;
        }
        if( ret.value.type==0){
            app.select_id_list=ret.value.data;
        }

    })

    api.addEventListener({
        name: 'do_open_neibubaoshi'
    }, function (ret, err) {
        ns.openWin('purchase_list', '申请记录');
    });

};
